package com.css.fxfzdzzh.modules.basicData.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzdzzh.constants.FxfzConstants;
import com.css.fxfzdzzh.constants.YNEnum;
import com.css.fxfzdzzh.modules.basicData.entity.Climate;
import com.css.fxfzdzzh.modules.basicData.landslideSite.entity.LandslideSiteFileData;
import com.css.fxfzdzzh.modules.basicData.param.ClimateParam;
import com.css.fxfzdzzh.util.CreateGson;
import com.css.fxfzdzzh.util.EasyPoiExcelUtil;
import com.css.fxfzdzzh.zcpt.sys.entity.SUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author Dbj
 * @date 2023年03月01日  13:40
 */
@Repository
public class ClimateRepository {

    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public List<Climate> getClimateData(ClimateParam param) {
        StringBuilder sql = new StringBuilder("select * from dzzh_sys_climate ");
        StringBuilder whereSql = new StringBuilder(" where del_flag = '"+ YNEnum.N.toString() +"'");
        dataPageSqlBuilder(param,sql);
        String sqlWhole = sql.append(whereSql).append(" order by create_time desc").toString();
        List<Climate> list = jdbcTemplate.query(sqlWhole, new BeanPropertyRowMapper<>(Climate.class));
        return list;
    }

    private void dataPageSqlBuilder(ClimateParam param, StringBuilder sqlBuilder) {
        if (StringUtils.isNotBlank(param.getProvince())) {
            sqlBuilder.append(" and province ='").append(param.getProvince()).append("'");
        }
        if (StringUtils.isNotBlank(param.getCity())) {
            sqlBuilder.append(" and city ='").append(param.getCity()).append("'");
        }
        if (StringUtils.isNotBlank(param.getCounty())) {
            sqlBuilder.append(" and county ='").append(param.getCounty()).append("'");
        }
    }

    public void exportExcel(List<Climate> list, HttpServletResponse response) {
        EasyPoiExcelUtil.exportExcel(list, "气候数据", "气候数据", Climate.class, "气候数据.xlsx", response);
    }
}
